using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
 
public class SceneTool : EditorWindow
{
    [MenuItem("Owen/Tool")]
    //绘制窗口
    static void tranPoint()
    {
        SceneTool win = (SceneTool)EditorWindow.GetWindow(typeof(SceneTool), false, "Tool", false);
        win.Show();
    }
    void OnGUI()
    {
        //设置字体类型
        GUIStyle style1 = new GUIStyle();
        //字体大小为15
        style1.fontSize = 15;
        //字体颜色为灰白色
        style1.normal.textColor = new Color(0.7f, 0.7f, 0.7f);
 
        GUIStyle style2 = new GUIStyle();
        style2.fontSize = 13;
        style2.normal.textColor = new Color(0.7f, 0.7f, 0.7f);
 
        //垂直绘制
        GUILayout.BeginHorizontal();
        {
            //lab的绘制
            EditorGUILayout.LabelField("▼TransformPoint", style1);
 
 
            //控制坐标位置的按钮绘制
 
            if (GUI.Button(new Rect(60, 40, 70, 25), "Top"))
            {
                if (Selection.activeGameObject)
                    TopPoint();
 
            }
            if (GUI.Button(new Rect(60, 70, 70, 25), "Center"))
            {
                if (Selection.activeGameObject)
                    CenterPoint();
            }
            if (GUI.Button(new Rect(60, 100, 70, 25), "Button"))
            {
                if (Selection.activeGameObject)
                    Buttom();
            }
 
        }
    }
 
        //坐标放在物体中心
        void CenterPoint()
        {
 
            Transform parent = Selection.activeGameObject.transform;
            if (parent.GetComponent<Renderer>() != null)
            {
                GameObject parent02 = new GameObject("A");
 
                parent02.transform.position = parent.position;
                parent02.transform.SetParent(parent.parent);
                parent.SetParent(parent02.transform);
                Selection.activeGameObject = parent02;
                parent = parent02.transform;
 
            }
 
            Vector3 position = parent.position;
            Vector3 scale = parent.localScale;
            //Vector3 rotate = parent.localEulerAngles;
 
 
            parent.position = Vector3.zero;
            parent.localScale = Vector3.one;
            //parent.localEulerAngles = Vector3.zero;
 
 
        Renderer[] m_renderer = parent.GetComponentsInChildren<Renderer>();
        Vector3 center = Vector3.zero;
 
        foreach (Renderer child in m_renderer)
        {
            center += child.bounds.center;
        }
        center /= m_renderer.Length;
 
 
 
        Bounds m_bounds = new Bounds(center, Vector3.zero);
        foreach (Renderer b in m_renderer)
        {
            m_bounds.Encapsulate(b.bounds);
        }
        foreach (Transform t in parent)
        {
            t.position = t.position - m_bounds.center;
        }
 
        parent.position = position + m_bounds.center;
        parent.localScale = scale;
        //parent.localEulerAngles = rotate;
        }
 
        //坐标放在物体中心的最顶端
        void TopPoint()
        {
            Transform parent = Selection.activeGameObject.transform;
            if (parent.GetComponent<Renderer>() != null)
            {
                GameObject parent02 = new GameObject("A");
 
                parent02.transform.position = parent.position;
                parent02.transform.SetParent(parent.parent);
                parent.SetParent(parent02.transform);
                Selection.activeGameObject = parent02;
                parent = parent02.transform;
            }
 
            Vector3 position = parent.position;
            Vector3 scale = parent.localScale;
            //Vector3 rotate = parent.localEulerAngles;
 
 
            parent.position = Vector3.zero;
            parent.localScale = Vector3.one;
            //parent.localEulerAngles = Vector3.zero;
 
 
            Renderer[] m_renderer = parent.GetComponentsInChildren<Renderer>();
            Vector3 center = Vector3.zero;
 
            foreach (Renderer child in m_renderer)
            {
                center += child.bounds.center;
            }
            center /= m_renderer.Length;
 
 
            Bounds m_bounds = new Bounds(center, Vector3.zero);
            foreach (Renderer b in m_renderer)
            {
                m_bounds.Encapsulate(b.bounds);
            }
 
            Vector3 V = m_bounds.center + new Vector3(0, m_bounds.size.y / 2, 0);
 
            foreach (Transform t in parent)
            {
                t.position -= V;
            }
 
            parent.position = position + V;
            parent.localScale = scale;
            //parent.localEulerAngles = rotate;
        }
 
        //坐标放在物体中心的最底端
        void Buttom()
        {
            Transform parent = Selection.activeGameObject.transform;
            if (parent.GetComponent<Renderer>() != null)
            {
                GameObject parent02 = new GameObject("A");
 
                parent02.transform.position = parent.position;
                parent02.transform.SetParent(parent.parent);
                parent.SetParent(parent02.transform);
                Selection.activeGameObject = parent02;
                parent = parent02.transform;
            }
 
            Vector3 position = parent.position;
            Vector3 scale = parent.localScale;
            //Vector3 rotate = parent.localEulerAngles;
 
 
            parent.position = Vector3.zero;
            parent.localScale = Vector3.one;
            //parent.localEulerAngles = Vector3.zero;
 
 
            Renderer[] m_renderer = parent.GetComponentsInChildren<Renderer>();
            Vector3 center = Vector3.zero;
 
            foreach (Renderer child in m_renderer)
            {
                center += child.bounds.center;
            }
            center /= m_renderer.Length;
 
 
            Bounds m_bounds = new Bounds(center, Vector3.zero);
            foreach (Renderer b in m_renderer)
            {
                m_bounds.Encapsulate(b.bounds);
            }
 
            Vector3 V = m_bounds.center - new Vector3(0, m_bounds.size.y / 2, 0);
 
            foreach (Transform t in parent)
            {
                t.position -= V;
            }
 
            parent.position = position + V;
            parent.localScale = scale;
            //parent.localEulerAngles = rotate;
        }
 
    
}